package com.stox.ui.javafx.charting.draw.mode;

import javafx.scene.input.MouseEvent;

import org.springframework.stereotype.Component;

import com.stox.ui.javafx.charting.draw.shape.AbstractShape;
import com.stox.ui.javafx.charting.draw.shape.TrendLine;
import com.stox.ui.javafx.charting.widget.ResizableCanvas;

@Component
public class TrendLineDrawingMode  extends AbstractDrawingMode  {

	@Override
	public void handle(MouseEvent event) {
		if(event.getSource() instanceof ResizableCanvas){
			ResizableCanvas canvas = (ResizableCanvas)event.getSource();
			if(MouseEvent.MOUSE_PRESSED.equals(event.getEventType())){
				TrendLine line = new TrendLine(canvas);
				line.getPoint().setX(event.getX());
				line.getPoint().setY(event.getY());
				line.getEditPoint().setX(event.getX());
				line.getEditPoint().setY(event.getY());
				canvas.getChart().getShapes().add(line);
			}else if(MouseEvent.MOUSE_DRAGGED.equals(event.getEventType())){
				for(AbstractShape shape : canvas.getChart().getShapes()){
					if(shape instanceof TrendLine){
						TrendLine line = (TrendLine)shape;
						if(!line.isFinished()){
							line.getEditPoint().setX(event.getX());
							line.getEditPoint().setY(event.getY());
						}
					}
				}
			}else if(MouseEvent.MOUSE_RELEASED.equals(event.getEventType())){
				for(AbstractShape shape : canvas.getChart().getShapes()){
					if(shape instanceof TrendLine){
						TrendLine line = (TrendLine)shape;
						line.setFinished(true);
					}
				}
			}
		}
	}

}
